package com.qumitech.visualnovel.service.account;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.datasource.console.UserInfoMapper;

@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getCurUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo userInfo = (UserInfo)subject.getPrincipal();
        return userInfo;
    }

    @Override
    public UserInfo getUserByUid(Integer uid) {
        return userInfoMapper.getUserInfo(uid, null);
    }

    @Override
    public UserInfo getUserByName(String username) {
        return userInfoMapper.getUserInfo(null, username);
    }

    @Override
    public int addNewUser(String password,String username) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        String salt = secureRandomNumberGenerator.nextBytes().toHex();
        Md5Hash md5 = new Md5Hash(password,username+salt,2);
        String newMd5Password = md5.toHex();
        int res = userInfoMapper.insertNewUser(newMd5Password,username,salt);
        return res;
    }

    @Override
    public void delUserByUid(int uid) {
        userInfoMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public int resetPassword(UserInfo userInfo, String password) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        Md5Hash md5 = new Md5Hash(password,userInfo.getUsername()+userInfo.getSalt(),2);
        String newMd5Password = md5.toHex();
        userInfo.setPassword(newMd5Password);
        int ret = userInfoMapper.updateByPrimaryKey(userInfo);
        return ret;
    }

    @Override
    public Boolean UpdatePassword(UserInfo userInfo, String oldPassword, String newPassword) {
        SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
        Md5Hash md5 = new Md5Hash(oldPassword, userInfo.getUsername()+userInfo.getSalt(), 2);
        String oldPasswordBySalt = md5.toHex();
        if (oldPasswordBySalt.equals(userInfo.getPassword())) {
            Md5Hash newMd5 = new Md5Hash(newPassword, userInfo.getUsername()+userInfo.getSalt(), 2);
            String newMd5Password = newMd5.toHex();
            userInfo.setPassword(newMd5Password);
            userInfoMapper.updateByPrimaryKey(userInfo);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int getUserInfoCount(String username, String startTime, String endTime) {
        return userInfoMapper.getUserInfoCount(username, startTime, endTime);
    }

    @Override
    public List<UserInfo> getUserInfoList(int offset, int pageSize, String username,
        String startTime, String endTime, String orderBy, String orderType) {
        return userInfoMapper.getUserInfoList(offset, pageSize, username, startTime, endTime, orderBy, orderType);
    }
}

